package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.utils.DateUtil;

public class FirstActivity extends AppCompatActivity {

    private TextView tv_return;
    private TextView content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        tv_return = findViewById(R.id.tv_return);
        content = findViewById(R.id.content);
        Button send = findViewById(R.id.send);
        send.setOnClickListener(new MyClick());

        PackageManager manager = getPackageManager();
        try {
            ActivityInfo activityInfo = manager.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
            Bundle bundle = activityInfo.metaData;
            String weather = bundle.getString("weather");
            Log.d("11111111111111",weather );
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }



//        接受返回消息
//@Override
//protected void onActivityResult(int requestCode, int resultCode, Intent intent)  // 接收返回数据
//    {
//    super.onActivityResult(requestCode, resultCode, intent);
//
//    // 意图非空，且请求代码为之前传的0，结果代码也为成功
//    if (intent!=null && requestCode==0 && resultCode== Activity.RESULT_OK)
//    {
//        Bundle bundle = intent.getExtras(); // 从返回的意图中获取快递包裹
//
//        // 从包裹中取出名叫response_time的字符串
//        String response_time = bundle.getString("response_time");
//
//        // 从包裹中取出名叫response_content的字符串
//        String response_content = bundle.getString("response_content");
//
//        String desc = String.format("收到返回消息：\n应答时间为：%s\n应答内容为：%s", response_time, response_content);
//
//        tv_response.setText(desc); // 把返回消息的详情显示在文本视图上
//    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Log.d("zhixing", "onActivityResult: 我执行了");
        //获取返回数据
        if (intent != null && requestCode == 0 && resultCode == Activity.RESULT_OK) {
            Bundle bundle = intent.getExtras();
            String return_content = bundle.getString("return_content");
            String request_time = bundle.getString("request_time");
            tv_return.setText(String.format("返回时间为:%s\n返回内容为:%s", request_time, return_content));
        }
    }

    class MyClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("request_content", (String) content.getText());
            bundle.putString("request_time", DateUtil.getNowtime());
            intent.putExtras(bundle);
            //跳转设置
            startActivityForResult(intent, 0);
        }

    }


}